package net.asg.games.yokeltowers.view;

import net.asg.games.yokeltowers.images.ImageProvider;
import net.asg.games.yokeltowers.sounds.SoundManager;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.utils.Array;

public class ColumnYokelGameObject extends YokelGameObject {
	public Array<BlockYokelGameObject> column;
	
		public ColumnYokelGameObject(ImageProvider imageProvider,
				TextureRegion[] textureRegions, SoundManager soundManager,
				int width, int height, boolean isHitboxActive, BlockYokelGameObject block1,
				BlockYokelGameObject block2, BlockYokelGameObject block3) {
			super(imageProvider, textureRegions, soundManager, width, height * 3,
					isHitboxActive);
			
			column = new Array<BlockYokelGameObject>();
			block2.rect.setY(block1.rect.y + block1.rect.height);
			block3.rect.setY(block1.rect.y + block1.rect.height * 2);

			column.add(block1);
			column.add(block2);
			column.add(block3);
		}		
		
		public void draw(SpriteBatch batch) {
			for (BlockYokelGameObject block : column){
				block.draw(batch);
			}
		}
		
		public void moveLeft(float delta, float speedBonus, Rectangle bounds) {
			rect.x -= rect.width + 1;
			column.get(0).rect.x -= rect.width;
			column.get(1).rect.x -= rect.width;
			column.get(2).rect.x -= rect.width;
			keepOnScreen(bounds);
		}
		
		public void moveDown(float delta, float speedBonus, Rectangle bounds) {
			rect.y -= moveSpeed * delta * speedBonus;
			column.get(0).rect.y -= moveSpeed * delta * speedBonus;
			column.get(1).rect.y -= moveSpeed * delta * speedBonus;
			column.get(2).rect.y -= moveSpeed * delta * speedBonus;
			keepOnScreen(bounds);
		}
		
		public void moveRight(float delta, float speedBonus, Rectangle bounds) {
			rect.x += rect.width + 1;
			column.get(0).rect.x += rect.width;
			column.get(1).rect.x += rect.width;
			column.get(2).rect.x += rect.width;
			keepOnScreen(bounds);
		}
		
		public void setX(int x){
			rect.setX(x);
			column.get(0).rect.setX(x);
			column.get(1).rect.setX(x);
			column.get(2).rect.setX(x);
		}
		
		public void setY(int y){
			rect.setY(y);
			column.get(0).rect.setY(y);
			column.get(1).rect.setY(y + column.get(0).rect.height);
			column.get(2).rect.setY(y + column.get(0).rect.height * 2);
		}
		
	    protected void keepOnScreen(Rectangle bounds) {
	        if (rect.x < bounds.x || column.get(0).rect.x < bounds.x || column.get(1).rect.x < bounds.x || 
	        		column.get(2).rect.x < bounds.x) {
				rect.setX(bounds.x);
				column.get(0).rect.setX(bounds.x);
				column.get(1).rect.setX(bounds.x);
				column.get(2).rect.setX(bounds.x);
	        }
	        else if (rect.x + width > (bounds.width + bounds.x ) || column.get(0).rect.x > (bounds.width + bounds.x ) || 
	        		column.get(1).rect.x > (bounds.width + bounds.x ) || column.get(2).rect.x > (bounds.width + bounds.x )) {
				rect.setX((bounds.width + bounds.x ) - width);
				column.get(0).rect.setX((bounds.width + bounds.x ) - width);
				column.get(1).rect.setX((bounds.width + bounds.x ) - width);
				column.get(2).rect.setX((bounds.width + bounds.x ) - width);
	        }
	    }
	    
	    public void cycleDown(){
	    	BlockYokelGameObject tempBlock = column.get(0);
	    	float y = column.get(0).rect.y;
	    	int h = column.get(0).height;
	    	column.set(0, column.get(1));	
	    	column.set(1, column.get(2));
	    	column.set(2, tempBlock);
	    	for (int x = 0; x < column.size; x++){
	    		column.get(x).rect.setY(y + h * x);
	    	}
	    }
	    
	    public void cycleUp(){
	    	BlockYokelGameObject tempBlock = column.get(2);
	    	float y = column.get(0).rect.y;
	    	int h = column.get(0).height;
	    	column.set(2, column.get(1));	
	    	column.set(1, column.get(0));
	    	column.set(0, tempBlock);
	    	for (int x = 0; x < column.size; x++){
	    		column.get(x).rect.setY(y + h * x);
	    	}
	    }
	    
	    public String toString(){
	    	String out = "*******\n";
			for (BlockYokelGameObject block : column){
				out += block.blockType + "\n";
			}
			out += "*******";
			return out;
	    }
}
